package com.koron.standard.jobStock.repair.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.standard.jobStock.repair.bean.dto.RepairItemDTO;
import com.koron.standard.jobStock.repair.bean.po.RepairItem;
import com.koron.standard.jobStock.repair.bean.po.RepairItemParams;
import com.koron.standard.jobStock.repair.mapper.RepairItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 计划检修标准下工作项目(RepairItem)表服务实现类
 *
 * @author jzp
 * @since 2021-09-16
 */
@Service
public class RepairItemService extends ServiceImpl<RepairItemMapper, RepairItem> {

    @Autowired
    private RepairItemParamsService repairItemParamsService;

    public List<RepairItemDTO> getDtoByRepairId(String repairId) {
        return getBaseMapper().selectDtoListByRepairId(repairId);
    }

    public List<RepairItemDTO> getTreeByRepairId(String repairId) {
        return toTree(getDtoByRepairId(repairId));
    }

    /**
     * DTO > tree
     */
    public List<RepairItemDTO> toTree(List<RepairItemDTO> itemDtos) {
        List<RepairItemDTO> itemDtoRoots = itemDtos.stream().filter(dto -> "0".equals(dto.getParentId())).collect(Collectors.toList());
        for (RepairItemDTO itemDtoRoot : itemDtoRoots) {
            List<RepairItemDTO> children = new ArrayList<>();
            for (int i = 0; i < itemDtos.size(); i++) {
                if (itemDtoRoot.getId().equals(itemDtos.get(i).getParentId())) {
                    children.add(itemDtos.get(i));
                    itemDtos.remove(i);
                    i = i - 1;
                }
            }
            itemDtoRoot.setChildren(children);
        }
        return itemDtoRoots;
    }

    public void saveRootDto(RepairItemDTO repairItemDTO) {
        saveDto(repairItemDTO, "0");
    }

    public void saveDto(RepairItemDTO repairItemDTO, String parentId) {

        // 1. 保存工作项目
        repairItemDTO.setParentId(parentId);
        save(repairItemDTO);

        String repairId = repairItemDTO.getRepairId();
        String repairItemId = repairItemDTO.getId();

        // 2. 保存记录项
        List<RepairItemParams> repairItemParams = repairItemDTO.getItemParams();
        // 设置作业标准id和作业标准项id
        for (RepairItemParams repairItemParam : repairItemParams) {
            repairItemParam.setRepairId(repairId);
            repairItemParam.setItemId(repairItemId);
        }
        repairItemParamsService.saveBatch(repairItemParams);
        List<RepairItemDTO> repairItemDTOS = Optional.ofNullable(repairItemDTO.getChildren()).orElse(new ArrayList<>());

        // 3. 保存子节点工作项目
        for (RepairItemDTO child : repairItemDTOS) {
            child.setRepairId(repairId);
            saveDto(child, repairItemId);
        }

    }

    public void removeByRepairId(String repairId) {
        if (StrUtil.isEmpty(repairId)) {
            return;
        }
        LambdaQueryWrapper<RepairItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairItem::getRepairId, repairId);
        List<RepairItem> list = list(wrapper);
        removeByIds(list.stream().map(RepairItem::getId).collect(Collectors.toList()));
        repairItemParamsService.removeByRepairId(repairId);
    }
}
